<script lang="ts" setup>
import { onMounted } from 'vue'
import { columns, searchFormSchema } from './salarySlip.data'
import { IconEnum } from '@/enums/appEnum'
import { useI18n } from '@/hooks/web/useI18n'
import { useMessage } from '@/hooks/web/useMessage'
import { BasicTable, TableAction, useTable } from '@/components/Table'
import {
  deleteSalarySlip,
  exportSalarySlip,
  getSalarySlipPage,
} from '@/api/wages/salaryslip'
import { useGo } from '@/hooks/web/usePage'
import { useModal } from '@/components/Modal'
import ImportModal from '@/views/wages/salarySlip/components/ImportModal.vue'

defineOptions({ name: 'SalarySlip' })

const go = useGo()
const { t } = useI18n()
const { createConfirm, createMessage } = useMessage()

const [registerTable, { getForm, reload }] = useTable({
  title: '工资单基本列表',
  api: getSalarySlipPage,
  columns,
  formConfig: { labelWidth: 120, schemas: searchFormSchema },
  useSearchForm: true,
  showTableSetting: true,
  actionColumn: {
    width: 140,
    title: t('common.action'),
    dataIndex: 'action',
    fixed: 'right',
  },
})

function handleEditTable(record: Recordable) {
  go(`/salary-slip/salarySlipTable?id=${record.id}`)
}

function handleCreatTable() {
  go(`/salary-slip/salarySlipTable`)
}

async function handleExport() {
  createConfirm({
    title: t('common.exportTitle'),
    iconType: 'warning',
    content: t('common.exportMessage'),
    async onOk() {
      await exportSalarySlip(getForm().getFieldsValue())
      createMessage.success(t('common.exportSuccessText'))
    },
  })
}

async function handleDelete(record: Recordable) {
  await deleteSalarySlip(record.id)
  createMessage.success(t('common.delSuccessText'))
  reload()
}

const [registerImportModal, { openModal: openImportModal, closeModal }] = useModal()

onMounted(async () => {
  await reload()
})
</script>

<template>
  <div>
    <BasicTable @register="registerTable">
      <template #toolbar>
        <a-button v-auth="['wages:salary-slip:create']" type="primary" :pre-icon="IconEnum.ADD" @click="handleCreatTable">
          {{ t('action.create') }}
        </a-button>
        <a-button v-auth="['wages:salary-slip:import']" :pre-icon="IconEnum.IMPORT" @click="openImportModal(true)">
          {{ t('action.import') }}
        </a-button>
        <!--        <a-button v-auth="['wages:salary-slip:export']" :pre-icon="IconEnum.EXPORT" @click="handleExport">
          {{ t('action.export') }}
        </a-button> -->
      </template>
      <template #bodyCell="{ column, record }">
        <template v-if="column.key === 'action'">
          <TableAction
            :actions="[
              { icon: IconEnum.EDIT, label: t('action.edit'), auth: 'wages:salary-slip:update', onClick: handleEditTable.bind(null, record) },
              {
                icon: IconEnum.DELETE,
                danger: true,
                label: t('action.delete'),
                auth: 'wages:salary-slip:delete',
                popConfirm: {
                  title: t('common.delMessage'),
                  placement: 'left',
                  confirm: handleDelete.bind(null, record),
                },
              },
            ]"
          />
        </template>
      </template>
    </BasicTable>
    <import-modal @register="registerImportModal" @success="reload()" @cancel="reload()" />
  </div>
</template>
